package com.earnings.okhttputils.utils.god.GodAdapter;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/5/20 0020.
 */

public abstract class GodBaseAdapter<T> extends RecyclerView.Adapter {
    public static final int TYPE_HEADER = 0;
    public static final int TYPE_NORMAL = 1;

    private int layoutId;
    private ArrayList<T> list = new ArrayList<>();
    private ArrayList<GodData> oneChoice;
    private GodArrayList<T> godArrayList;
    private setOnClickListener ItemOnClickListener;
    private View mHeaderView;
    private boolean HeaderViewLine;
    private boolean bindingList;
    private boolean isOneChoice;
    private boolean isMoreChoose;
    private int selectColor, noSelectColor;
    private int selectResource, noselectResource;
    private boolean drawbleMode;

    public GodBaseAdapter(int layoutId) {
        this.layoutId = layoutId;
    }

    public GodBaseAdapter(int layoutId, ArrayList<T> list) {
        this.layoutId = layoutId;
        if (list != null) {
            if (list instanceof GodArrayList) {
                this.godArrayList = (GodArrayList<T>) list;
                godArrayList.setAdapter(this);
                bindingList = true;
            } else {
                this.list = list;
            }

        }
    }

    /**
     * 开启多选模式
     */
    public void setMoreChoose(boolean isMoreChoose) {
        this.isMoreChoose = isMoreChoose;
        this.isOneChoice = !isMoreChoose;
        if (isMoreChoose) isOneChoice = false;
        if (isMoreChoose && godArrayList.size() != 0 && godArrayList != null) {
            oneChoice = new ArrayList<GodData>();
            for (int i = 0; i < godArrayList.size(); i++) {
                oneChoice.add(new GodData(false));
            }
        }
    }


    /**
     * 开启单选模式
     */
    public void setOneChoice(boolean isOneChoice) {
        this.isOneChoice = isOneChoice;
        if (isOneChoice) isMoreChoose = false;
        if (isOneChoice && godArrayList.size() != 0 && godArrayList != null) {
            oneChoice = new ArrayList<GodData>();
            for (int i = 0; i < godArrayList.size(); i++) {
                oneChoice.add(new GodData(false));
            }
        }
    }

    //单选模式下默认选中第？个
    public void setDefaultposition(int position) {
        if (oneChoice != null) {
            for (int i = 0; i < oneChoice.size(); i++) {
                oneChoice.get(i).setOpt(false);
            }
            oneChoice.get(position).setOpt(true);
            defaultposition = position;
        }

    }

    //单选状态被选中的索引
    public int getOptindex() {
        if (getOneChoice() == null || getOneChoice().size() == 0) return -1;
        for (int i = 0; i < getOneChoice().size(); i++) {
            GodData data = getOneChoice().get(i);
            if (data.isOpt()) return i;
        }
        return -1;
    }

    private int defaultposition = 0;

    public void setChoiceDrawableBackground(int selectResource, int noselectResource) {
        this.selectResource = selectResource;
        this.noselectResource = noselectResource;
        drawbleMode = true;
    }

    //选择模式下的颜色
    public void setChoiceColor(int selectColor, int noSelectColor) {
        this.selectColor = selectColor;
        this.noSelectColor = noSelectColor;
    }

    //返回选择模式的集合
    public ArrayList<GodData> getOneChoice() {
        if(oneChoice==null||oneChoice.size()!=godArrayList.size()){
            setOneChoice(true);
        }
        return oneChoice;
    }


    public void setOneChoice(ArrayList<GodData> oneChoice) {
        this.oneChoice = oneChoice;
    }

    public boolean getIsOneChoice() {
        return isOneChoice;
    }

    public boolean getIsMoreChoose() {
        return isMoreChoose;
    }

    public int getDefaultposition() {
        return defaultposition;
    }

    /**
     * 设置第一个Item为一整行
     *
     * @param HeaderViewLine
     */
    public void setHeaderViewLine(boolean HeaderViewLine) {
        this.HeaderViewLine = HeaderViewLine;
    }

    //是否绑定了GodArrayList
    public boolean isBindingList() {
        return bindingList;
    }

    //设置数据
    public void setDatas(List<T> list) {
        if (!bindingList) this.list.addAll(list);
        else godArrayList.addAll(list);
        notifyDataSetChanged();
    }

    public View getHeaderView() {
        return mHeaderView;
    }

    public void setHeaderView(View headerView) {
        mHeaderView = headerView;
        notifyItemInserted(0);
    }

    @Override
    public int getItemViewType(int position) {
        if (mHeaderView == null) return TYPE_NORMAL;
        if (position == 0) return TYPE_HEADER;
        return TYPE_NORMAL;
    }

    public ArrayList<T> getDatas() {
        if (bindingList) return godArrayList;
        return list;
    }

    @Override
    public GodViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mHeaderView != null && viewType == TYPE_HEADER) return new GodViewHolder(mHeaderView);
        return new GodViewHolder(LayoutInflater.from(parent.getContext()).inflate(layoutId, parent, false));
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        if (getItemViewType(position) == TYPE_HEADER) return;
        final int post = getRealPosition(holder);
        if (holder instanceof GodViewHolder) {
            T data;
            if (bindingList) data = godArrayList.get(post);
            else data = list.get(post);
            onBind((GodViewHolder) holder, post, data);
            //绘制单选
            drawOptColor(post, holder);
            //绘制多选
            drawMoreColor(post, holder);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (isOneChoice || isMoreChoose && oneChoice != null) setOptColor(post);
                    T data;
                    if (bindingList) data = godArrayList.get(post);
                    else data = list.get(post);
                    if (ItemOnClickListener != null) ItemOnClickListener.ItemOnClickListener(holder.itemView, post, data);
                    OnItemClickListener(holder.itemView, post, data);

                }
            });

        }
    }

    private void drawOptColor(int position, RecyclerView.ViewHolder holder) {
        //开启单选模式同时数据不为空时
        if (isOneChoice && oneChoice != null) {
            boolean opt = oneChoice.get(position).isOpt();
            if (opt) holder.itemView.setBackgroundColor(selectColor);
            else holder.itemView.setBackgroundColor(noSelectColor);
        }
    }

    private void drawMoreColor(int position, RecyclerView.ViewHolder holder) {
        //开启多选模式同时数据不为空时
        if (isMoreChoose && oneChoice != null) {
            boolean opt = oneChoice.get(position).isOpt();
            if (drawbleMode) {
                if (opt) holder.itemView.setBackgroundResource(selectResource);
                else holder.itemView.setBackgroundResource(noselectResource);
            } else {
                if (opt) holder.itemView.setBackgroundColor(selectColor);
                else holder.itemView.setBackgroundColor(noSelectColor);
            }

        }
    }

    private void setOptColor(int position) {
        //单选
        if (isOneChoice) {
            if (oneChoice == null || oneChoice.size() == 0) {
                setOneChoice(true);
            }
            for (int i = 0; i < oneChoice.size(); i++) {
                oneChoice.get(i).setOpt(false);
            }
            oneChoice.get(position).setOpt(true);
        }
        //多选
        else {
            oneChoice.get(position).setOpt(!oneChoice.get(position).isOpt());
        }
        //更新Item背景色
        notifyDataSetChanged();
    }


    //内部类监听
    public void OnItemClickListener(View view, int position, T data) {

    }

    //外部接口监听
    public void setOnItemClickListener(setOnClickListener onClickListener) {
        ItemOnClickListener = onClickListener;
    }

    public interface setOnClickListener<T> {
        void ItemOnClickListener(View view, int position, T data);
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        if (!HeaderViewLine) return;
        super.onAttachedToRecyclerView(recyclerView);
        RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
        if (manager instanceof GridLayoutManager) {
            final GridLayoutManager gridManager = ((GridLayoutManager) manager);
            gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    return getItemViewType(position) == TYPE_HEADER ? gridManager.getSpanCount() : 1;
                }
            });
        }
    }

    @Override
    public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) {
        if (!HeaderViewLine) return;
        super.onViewAttachedToWindow(holder);
        ViewGroup.LayoutParams lp = holder.itemView.getLayoutParams();
        if (lp != null && lp instanceof StaggeredGridLayoutManager.LayoutParams && holder.getLayoutPosition() == 0) {
            StaggeredGridLayoutManager.LayoutParams p = (StaggeredGridLayoutManager.LayoutParams) lp;
            p.setFullSpan(true);
        }
    }


    public int getRealPosition(RecyclerView.ViewHolder holder) {
        int position = holder.getLayoutPosition();
        return mHeaderView == null ? position : position - 1;
    }

    @Override
    public int getItemCount() {
        if (bindingList) return mHeaderView == null ? godArrayList.size() : godArrayList.size() + 1;
        else return mHeaderView == null ? list.size() : list.size() + 1;
    }

    public abstract void onBind(GodViewHolder holder, int position, T data);
}
